package pers.zh.parking.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.model.LatLng;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import pers.zh.parking.R;
import pers.zh.parking.amap.NaviInfoCallback;
import pers.zh.parking.common.ApiService;
import pers.zh.parking.common.Const;
import pers.zh.parking.common.GlideImageLoader;
import pers.zh.parking.di.component.DaggerLotDetailComponent;
import pers.zh.parking.data.entity.ParkingLot;
import pers.zh.parking.mvp.contract.LotDetailContract;
import pers.zh.parking.mvp.presenter.LotDetailPresenter;
import pers.zh.parking.utils.MyUtils;
import pers.zjc.commonlibs.util.ToastUtils;

import static com.jess.arms.utils.Preconditions.checkNotNull;

public class LotDetailActivity extends BaseActivity<LotDetailPresenter>
        implements LotDetailContract.View, View.OnClickListener {

    @BindView(R.id.ivLeft)
    ImageView ivLeft;
    @BindView(R.id.toolbar_left)
    RelativeLayout toolbarLeft;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tvDistance)
    TextView tvDistance;
    @BindView(R.id.tvSpaceFree)
    TextView tvSpaceFree;
    @BindView(R.id.tvSpaceTotal)
    TextView tvSpaceTotal;
    @BindView(R.id.cv_lot)
    CardView cvLot;
    @BindView(R.id.llPrice)
    LinearLayout llPrice;
    @BindView(R.id.tvReserve)
    TextView tvReserve;
    @BindView(R.id.tvNavigate)
    TextView tvNavigate;

    private ParkingLot lot;

    private LatLng curLoc;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLotDetailComponent //如找不到该类,请编译一下项目
                                 .builder()
                                 .appComponent(appComponent)
                                 .view(this)
                                 .build()
                                 .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_lot_detail;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        initArgs();
        initView();
        fillData();
    }

    private void fillData() {
        if (lot == null) {
            return;
        }
        tvTitle.setText(lot.getName());
        tvDistance.setText(String.valueOf(lot.getDistance()));
        tvSpaceFree.setText(String.valueOf(lot.getSpaceFree()));
        tvSpaceTotal.setText(String.valueOf(lot.getSpaceTotal()));
        Map<String, String> map = new LinkedHashMap<>();
        map.put("收费标准", lot.getPriceStr());
        map.put("支付方式", lot.getPayWayStr());
        //        llPrice.removeAllViews();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            View priceView = LayoutInflater.from(this)
                                           .inflate(R.layout.item_hor_key_value, null, false);
            TextView tvKey = priceView.findViewById(R.id.tvKey);
            TextView tvValue = priceView.findViewById(R.id.tvValue);
            tvKey.setText(entry.getKey());
            tvValue.setText(entry.getValue());
            llPrice.addView(priceView);
        }
        List<String> urls = new ArrayList<>();
        List<String> titles = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            urls.add(ApiService.APP_DOMAIN + "file/download1");
            titles.add(lot.getLocation());
        }
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(urls);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.Default);
        //设置标题集合（当banner样式有显示title时）
        banner.setBannerTitles(titles);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    private void initArgs() {
        Intent intent = getIntent();
        if (intent == null) {
            showMessage("参数错误");
            killMyself();
            return;
        }
        lot = intent.getParcelableExtra(Const.Keys.KEY_PARKING_LOT);
        curLoc = intent.getParcelableExtra(Const.Keys.KEY_CUR_LOCATION);
    }

    private void initView() {
        initBanner();
        ivLeft.setOnClickListener(this);
        tvReserve.setOnClickListener(this);
        tvNavigate.setOnClickListener(this);
    }

    private void initBanner() {
        // 用代码创建的banner无法显示指示器，换为使用布局创建
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        //显示圆形指示器和标题（水平显示)
        banner.setOnBannerListener(position -> {

        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ToastUtils.showShort(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivLeft:
                killMyself();
                break;
            case R.id.tvReserve:
                mPresenter.reserve(lot.getLotId());
                break;
            case R.id.tvNavigate:
                if (curLoc == null) {
                    showMessage("未获取到当前位置信息");
                    return;
                }
                MyUtils.startNavigate(LotDetailActivity.this, curLoc, new LatLng(lot.getLatitude(), lot.getLongitude()), lot.getName(), new NaviInfoCallback(
                        new NaviInfoCallback.OnArriveCallback() {
                            @Override
                            public void onArrive() {

                            }
                        }));
                break;
            default:
                break;
        }
    }

    @Override
    public void reserveSuccess() {
        showMessage("预约成功");
        Intent intent = new Intent(this, ReservationActivity.class);
        intent.putExtra(Const.Keys.KEY_CUR_LOCATION, curLoc);
        launchActivity(intent);
        finish();
    }
}
